
package database;

import core.COINCOMOComponent;
import core.COINCOMOSubComponent;
import core.COINCOMOComponent;
import core.COINCOMOUnit;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import javax.swing.JOptionPane;

import main.COINCOMO;

/**
 *
 * @author Raed Shomali
 */
public class COINCOMOComponentManager
{
	private static String insertComponentSQL( COINCOMOComponent component )
	{
		StringBuffer sql = new StringBuffer();

		sql.append( "SELECT Insert_Component(" );
		sql.append( "\'" + component.getName() + "\'," );
		sql.append( "" + component.getParentID() + ");" );

		return sql.toString();
	}

	public static float get_Total_Prod( long componentID , DBConnection connection )
	{
		float prod = 0;

		// Check if a Connection is available ..
		if ( connection != null )
		{
			try
			{
				StringBuffer sql = new StringBuffer();

				sql.append( "SELECT Get_Total_Prod(" );
				sql.append( "" + componentID + ");" );

				// Efficient Query Statement
				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql.toString() );

				ResultSet rs = preparedStatement.executeQuery();

				// Check if a Row is returned ..
				if ( rs != null && rs.next() )
				{
					prod = rs.getFloat( 1 );
				}

				// Release From Memory
				preparedStatement.close();
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
		}

		return prod;
	}

	public static float get_Total_Cost( long componentID , DBConnection connection )
	{
		float cost = 0;

		// Check if a Connection is available ..
		if ( connection != null )
		{
			try
			{
				StringBuffer sql = new StringBuffer();

				sql.append( "SELECT Get_Total_Cost(" );
				sql.append( "" + componentID + ");" );

				// Efficient Query Statement
				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql.toString() );

				ResultSet rs = preparedStatement.executeQuery();

				// Check if a Row is returned ..
				if ( rs != null && rs.next() )
				{
					cost = rs.getFloat( 1 );
				}

				// Release From Memory
				preparedStatement.close();
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
		}

		return cost;
	}

	public static float get_Total_Inst_Cost( long componentID , DBConnection connection )
	{
		float cost = 0;

		// Check if a Connection is available ..
		if ( connection != null )
		{
			try
			{
				StringBuffer sql = new StringBuffer();

				sql.append( "SELECT Get_Total_Inst_Cost(" );
				sql.append( "" + componentID + ");" );

				// Efficient Query Statement
				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql.toString() );

				ResultSet rs = preparedStatement.executeQuery();

				// Check if a Row is returned ..
				if ( rs != null && rs.next() )
				{
					cost = rs.getFloat( 1 );
				}

				// Release From Memory
				preparedStatement.close();
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
		}

		return cost;
	}

	public static float get_Total_Staff( long componentID , DBConnection connection )
	{
		float staff = 0;

		// Check if a Connection is available ..
		if ( connection != null )
		{
			try
			{
				StringBuffer sql = new StringBuffer();

				sql.append( "SELECT Get_Total_Staff(" );
				sql.append( "" + componentID + ");" );

				// Efficient Query Statement
				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql.toString() );

				ResultSet rs = preparedStatement.executeQuery();

				// Check if a Row is returned ..
				if ( rs != null && rs.next() )
				{
					staff = rs.getFloat( 1 );
				}

				// Release From Memory
				preparedStatement.close();
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
		}

		return staff;
	}

	public static float get_Total_Effort( long componentID , DBConnection connection )
	{
		float effort = 0;

		// Check if a Connection is available ..
		if ( connection != null )
		{
			try
			{
				StringBuffer sql = new StringBuffer();

				sql.append( "SELECT Get_PM_Aggregate(" );
				sql.append( "" + componentID + ");" );

				// Efficient Query Statement
				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql.toString() );

				ResultSet rs = preparedStatement.executeQuery();

				// Check if a Row is returned ..
				if ( rs != null && rs.next() )
				{
					effort = rs.getFloat( 1 );
				}

				// Release From Memory
				preparedStatement.close();
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
		}

		return effort;
	}

	public static float get_Total_Schedule( long componentID , DBConnection connection )
	{
		float schedule = 0;

		// Check if a Connection is available ..
		if ( connection != null )
		{
			try
			{
				StringBuffer sql = new StringBuffer();

				sql.append( "SELECT Get_TDEV(" );
				sql.append( "" + componentID + ");" );

				// Efficient Query Statement
				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql.toString() );

				ResultSet rs = preparedStatement.executeQuery();

				// Check if a Row is returned ..
				if ( rs != null && rs.next() )
				{
					schedule = rs.getFloat( 1 );
				}

				// Release From Memory
				preparedStatement.close();
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
		}

		return schedule;
	}

	public static float get_Scale_Factor( long componentID , DBConnection connection )
	{
		float scaleFactor = 0;

		// Check if a Connection is available ..
		if ( connection != null )
		{
			try
			{
				StringBuffer sql = new StringBuffer();

				sql.append( "SELECT Get_Scale_Factor(" );
				sql.append( "" + componentID + ");" );

				// Efficient Query Statement
				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql.toString() );

				ResultSet rs = preparedStatement.executeQuery();

				// Check if a Row is returned ..
				if ( rs != null && rs.next() )
				{
					scaleFactor = rs.getFloat( 1 );
				}

				// Release From Memory
				preparedStatement.close();
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
		}

		return scaleFactor;
	}

	public static String set_COPSEMO_S_Percent( long componentID , int phase , double percent )
	{
		StringBuffer sql = new StringBuffer();

		sql.append( "SELECT Set_COPSEMO_S_Percent(" );
		sql.append( "" + componentID + "," );
		sql.append( "" + phase + "," );
		sql.append( "" + percent + ");" );

		return sql.toString();
	}

	public static String set_COPSEMO_E_Percent( long componentID , int phase , double percent )
	{
		StringBuffer sql = new StringBuffer();

		sql.append( "SELECT Set_COPSEMO_E_Percent(" );
		sql.append( "" + componentID + "," );
		sql.append( "" + phase + "," );
		sql.append( "" + percent + ");" );

		return sql.toString();
	}

	public static String get_All_Phase_Effort( long componentID )
	{
		StringBuffer sql = new StringBuffer();

		sql.append( "SELECT Get_COPSEMO_E_Phase(" );
		sql.append( "" + componentID + ",1)," );
		sql.append( "Get_COPSEMO_E_Phase(" );
		sql.append( "" + componentID + ",2)," );
		sql.append( "Get_COPSEMO_E_Phase(" );
		sql.append( "" + componentID + ",3)," );
		sql.append( "Get_COPSEMO_E_Phase(" );
		sql.append( "" + componentID + ",4);" );

		return sql.toString();
	}

	public static String get_All_Phase_Effort_Percentage( long componentID )
	{
		StringBuffer sql = new StringBuffer();

		sql.append( "SELECT Get_COPSEMO_E_Percent(" );
		sql.append( "" + componentID + ",1)," );
		sql.append( "Get_COPSEMO_E_Percent(" );
		sql.append( "" + componentID + ",2)," );
		sql.append( "Get_COPSEMO_E_Percent(" );
		sql.append( "" + componentID + ",3)," );
		sql.append( "Get_COPSEMO_E_Percent(" );
		sql.append( "" + componentID + ",4);" );

		return sql.toString();
	}

	public static String get_All_Phase_Schedule_Percentage( long componentID )
	{
		StringBuffer sql = new StringBuffer();

		sql.append( "SELECT Get_COPSEMO_S_Percent(" );
		sql.append( "" + componentID + ",1)," );
		sql.append( "Get_COPSEMO_S_Percent(" );
		sql.append( "" + componentID + ",2)," );
		sql.append( "Get_COPSEMO_S_Percent(" );
		sql.append( "" + componentID + ",3)," );
		sql.append( "Get_COPSEMO_S_Percent(" );
		sql.append( "" + componentID + ",4);" );

		return sql.toString();
	}

	public static String get_All_Phase_Schedule( long componentID )
	{
		StringBuffer sql = new StringBuffer();

		sql.append( "SELECT Get_COPSEMO_S_Phase(" );
		sql.append( "" + componentID + ",1)," );
		sql.append( "Get_COPSEMO_S_Phase(" );
		sql.append( "" + componentID + ",2)," );
		sql.append( "Get_COPSEMO_S_Phase(" );
		sql.append( "" + componentID + ",3)," );
		sql.append( "Get_COPSEMO_S_Phase(" );
		sql.append( "" + componentID + ",4);" );

		return sql.toString();
	}

	public static COINCOMOComponent insertComponent( long componentID )
	{
		COINCOMOComponent component = null;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

		// Insert Only When a Connection is available ..
		if ( connection != null )
		{
			try
			{
				component = new COINCOMOComponent();
				component.setName( "(Component) " + componentID );
				component.setParentID( componentID );

				int scaleFactorsBase[] = new int[ 5 ];
				int scaleFactorsIncr[] = new int[ 5 ];

				for ( int i = 0 ; i < scaleFactorsBase.length ; i++ )
				{
					scaleFactorsBase[i] = 3;
					scaleFactorsIncr[i] = 0;
				}

				component.setScaleFactorsBase( scaleFactorsBase );
				component.setScaleFactorsIncr( scaleFactorsIncr );

				component.setScedBase( 3 );
				component.setScedIncr( 0 );

				// Default
				component.setScaleFactor( 18.97f );

				// SQL Insert
				String sql = insertComponentSQL( component );

				// For Better Performance Insert Statements
				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );

				// Do the insertion ...
				ResultSet rs = preparedStatement.executeQuery();

				try
				{
					// Get Row
					if ( rs.next() )
					{
						component.setUnitID( rs.getInt( 1 ) );
					}
				}
				catch ( SQLException se )
				{
					return null;
				}
				finally
				{
					// Release from memory ...
					preparedStatement.close();
				}
			}
			catch ( SQLException e )
			{
				return null;
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}
		}

		return component;
	}

	public static boolean updateComponent( COINCOMOComponent component )
	{
		Vector<COINCOMOUnit> vector = new Vector<COINCOMOUnit>();

		vector.add( component );

		return updateComponents( vector );
	}

	public static boolean updateComponents( Vector<COINCOMOUnit> components )
	{
		boolean isSuccessful = false;

		// If Contains Any
		if ( !components.isEmpty() )
		{
			DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

			if ( connection != null )
			{
				try
				{
					for ( int i = 0 ; i < components.size() ; i++ )
					{
						COINCOMOComponent component = (COINCOMOComponent) components.get( i );

						String sql = "update cocomo_projects_table set " +
						"project_Name = ? , " +
								"scale_factor_button.scale_factor_base[ 1 ] = ? ," +
								"scale_factor_button.scale_factor_inc[ 1 ] = ? ," +
								"scale_factor_button.scale_factor_base[ 2 ] = ? ," +
								"scale_factor_button.scale_factor_inc[ 2 ] = ? ," +
								"scale_factor_button.scale_factor_base[ 3 ] = ? ," +
								"scale_factor_button.scale_factor_inc[ 3 ] = ? ," +
								"scale_factor_button.scale_factor_base[ 4 ] = ? ," +
								"scale_factor_button.scale_factor_inc[ 4 ] = ? ," +
								"scale_factor_button.scale_factor_base[ 5 ] = ? ," +
								"scale_factor_button.scale_factor_inc[ 5 ] = ? ," +
								"schedule_button.post_arch_sched.base = ? , " +
								"schedule_button.post_arch_sched.incr = ? , " +
								"shift = ? " +
								"where project_ID = ?";

						// Efficient Way of Updating
						PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );

						// Replace "?" With Respective Values ..
						preparedStatement.setString( 1 , component.getName() );

						preparedStatement.setInt( 2 , component.getScaleFactorsBase()[ 0] );
						preparedStatement.setInt( 3 , component.getScaleFactorsIncr()[ 0] );
						preparedStatement.setInt( 4 , component.getScaleFactorsBase()[ 1] );
						preparedStatement.setInt( 5 , component.getScaleFactorsIncr()[ 1] );
						preparedStatement.setInt( 6 , component.getScaleFactorsBase()[ 2] );
						preparedStatement.setInt( 7 , component.getScaleFactorsIncr()[ 2] );
						preparedStatement.setInt( 8 , component.getScaleFactorsBase()[ 3] );
						preparedStatement.setInt( 9 , component.getScaleFactorsIncr()[ 3] );
						preparedStatement.setInt( 10 , component.getScaleFactorsBase()[ 4] );
						preparedStatement.setInt( 11 , component.getScaleFactorsIncr()[ 4] );

						preparedStatement.setInt( 12 , component.getScedBase() );
						preparedStatement.setInt( 13 , component.getScedIncr() );

						preparedStatement.setInt( 14 , component.getMultiBuildShift() );

						preparedStatement.setLong( 15 , component.getUnitID() );

						// Update
						preparedStatement.executeUpdate();

						// Update Generated Formulas
						component.setTotalProd( COINCOMOComponentManager.get_Total_Prod( component.getUnitID() , connection ) );
						component.setTotalCost( COINCOMOComponentManager.get_Total_Cost( component.getUnitID() , connection ) );
						component.setTotalInstCost( COINCOMOComponentManager.get_Total_Inst_Cost( component.getUnitID() , connection ) );
						component.setTotalStaff( COINCOMOComponentManager.get_Total_Staff( component.getUnitID() , connection ) );
						component.setTotalEffort( COINCOMOComponentManager.get_Total_Effort( component.getUnitID() , connection ) );
						component.setTotalSchedule( COINCOMOComponentManager.get_Total_Schedule( component.getUnitID() , connection ) );

						component.setScaleFactor( COINCOMOComponentManager.get_Scale_Factor( component.getUnitID() , connection ) );

						// Effort
						sql = set_COPSEMO_E_Percent( component.getUnitID() , 1 , component.getInceptionEffortPercentage() );
						preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
						preparedStatement.executeQuery();

						sql = set_COPSEMO_E_Percent( component.getUnitID() , 2 , component.getElaborationEffortPercentage() );
						preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
						preparedStatement.executeQuery();

						sql = set_COPSEMO_E_Percent( component.getUnitID() , 3 , component.getConstructionEffortPercentage() );
						preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
						preparedStatement.executeQuery();

						sql = set_COPSEMO_E_Percent( component.getUnitID() , 4 , component.getTransitionEffortPercentage() );
						preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
						preparedStatement.executeQuery();

						// Schedule
						sql = set_COPSEMO_S_Percent( component.getUnitID() , 1 , component.getInceptionSchedulePercentage() );
						preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
						preparedStatement.executeQuery();

						sql = set_COPSEMO_S_Percent( component.getUnitID() , 2 , component.getElaborationSchedulePercentage() );
						preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
						preparedStatement.executeQuery();

						sql = set_COPSEMO_S_Percent( component.getUnitID() , 3 , component.getConstructionSchedulePercentage() );
						preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
						preparedStatement.executeQuery();

						sql = set_COPSEMO_S_Percent( component.getUnitID() , 4 , component.getTransitionSchedulePercentage() );
						preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
						preparedStatement.executeQuery();


						sql = get_All_Phase_Effort( component.getUnitID() );
						preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
						ResultSet rs = preparedStatement.executeQuery();

						if ( rs != null && rs.next() )
						{
							component.setInceptionPM( Double.parseDouble( rs.getString( 1 ) == null ? "0" : rs.getString( 1 ) ) );
							component.setElaborationPM( Double.parseDouble( rs.getString( 2 ) == null ? "0" : rs.getString( 2 ) ) );
							component.setConstructionPM( Double.parseDouble( rs.getString( 3 ) == null ? "0" : rs.getString( 3 ) ) );
							component.setTransitionPM( Double.parseDouble( rs.getString( 4 ) == null ? "0" : rs.getString( 4 ) ) );
						}

						sql = get_All_Phase_Schedule( component.getUnitID() );
						preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
						rs = preparedStatement.executeQuery();

						if ( rs != null && rs.next() )
						{
							component.setInceptionM( Double.parseDouble( rs.getString( 1 ) == null ? "0" : rs.getString( 1 ) ) );
							component.setElaborationM( Double.parseDouble( rs.getString( 2 ) == null ? "0" : rs.getString( 2 ) ) );
							component.setConstructionM( Double.parseDouble( rs.getString( 3 ) == null ? "0" : rs.getString( 3 ) ) );
							component.setTransitionM( Double.parseDouble( rs.getString( 4 ) == null ? "0" : rs.getString( 4 ) ) );
						}

						// Free From Memory
						preparedStatement.close();
					}

					// Update Status
					isSuccessful = true;
				}
				catch ( SQLException e )
				{
					// Print Any Problems ..
					e.printStackTrace();
				}
				finally
				{
					// Disconnect
					COINCOMODatabaseManager.disconnect( connection );
				}
			}
		}

		return isSuccessful;
	}

	public static boolean deleteComponent( long componentID )
	{
		boolean isSuccessful = false;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

		// Check if connection exists
		if ( connection != null )
		{
			try
			{
				String sql = "delete from cocomo_projects_table where project_ID=?";

				// Efficent Deletion
				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );

				// Replace "?" With ID Value
				preparedStatement.setLong( 1 , componentID );

				// Delete
				preparedStatement.executeUpdate();

				// Release From Memory
				preparedStatement.close();

				// Update Status
				isSuccessful = true;
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}
		}

		return isSuccessful;
	}

	public static boolean loadComponent( COINCOMOComponent component , DBConnection connection )
	{
		// Check if a Connection is available ..
		if ( connection != null )
		{
			try
			{
				StringBuilder sql = new StringBuilder();

				sql.append( "Select  " +
						"module_Name , " +
						"labor_rate , " +
						"(module_size).breakage , " +
						"(module_size).sizing_typ , " +
						"(module_size).module_language , " +
						"(module_size).sloc , " +
						"(module_size).adaptation_and_reuse.initial_sloc  , " +
						"(module_size).adaptation_and_reuse.design_modified , " +
						"(module_size).adaptation_and_reuse.code_modified , " +
						"(module_size).adaptation_and_reuse.integration_modified , " +
						"(module_size).adaptation_and_reuse.software_understanding  , " +
						"(module_size).adaptation_and_reuse.assement_and_assimilation , " +
						"(module_size).adaptation_and_reuse.unfamiliarity_with_software  , " +
						"(module_size).adaptation_and_reuse.automatically_translated  , " +
						"(module_size).adaptation_and_reuse.auto_translation_productivity  , " +
						"(module_size).function_points.change_multiplier  , " +
						"(module_size).function_points.internal_logical_files.low  , " +
						"(module_size).function_points.internal_logical_files.average  , " +
						"(module_size).function_points.internal_logical_files.high  , " +
						"(module_size).function_points.external_interface_files.low  , " +
						"(module_size).function_points.external_interface_files.average  , " +
						"(module_size).function_points.external_interface_files.high , " +
						"(module_size).function_points.external_inputs.low , " +
						"(module_size).function_points.external_inputs.average , " +
						"(module_size).function_points.external_inputs.high , " +
						"(module_size).function_points.external_outputs.low , " +
						"(module_size).function_points.external_outputs.average , " +
						"(module_size).function_points.external_outputs.high  , " +
						"(module_size).function_points.external_inquiries.low  , " +
						"(module_size).function_points.external_inquiries.average  , " +
				"(module_size).function_points.external_inquiries.high  , " );

				for ( int i = 1 ; i <= 18 ; i++ )
				{
					sql.append( " (eaf).post_architecture.effort_multiplier_base[ " + i + " ] , " +
							" (eaf).post_architecture.effort_multiplier_inc[ " + i + " ] , " );
				}

				sql.append( " module_id " );
				sql.append( " from sub_components_table where project_id = " + component.getUnitID() );

				// Efficient Query Statement
				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql.toString() );

				ResultSet rs = preparedStatement.executeQuery();

				// Loop through ..
				while ( rs != null && rs.next() )
				{
					String name = rs.getString( 1 );
					float laborRate = rs.getFloat( 2 );
					float breakage = rs.getFloat( 3 );
					int sizeType = rs.getInt( 4 );
					String language = rs.getString( 5 );
					long SLOC = rs.getLong( 6 );
					long initialSLOC = rs.getLong( 7 );
					float designModified = rs.getFloat( 8 );
					float codeModified = rs.getFloat( 9 );
					float integrationModified = rs.getFloat( 10 );
					float softwareUnderstanding = rs.getFloat( 11 );
					float assessment = rs.getFloat( 12 );
					float unfamiliarity = rs.getFloat( 13 );
					float automaticallyTranslated = rs.getFloat( 14 );
					float autoTranslation = rs.getFloat( 15 );
					long change = rs.getLong( 16 );

					long internalLogicalFiles[] = new long[ 3 ];
					long externalInterfaceFiles[] = new long[ 3 ];
					long externalInputs[] = new long[ 3 ];
					long externalOutputs[] = new long[ 3 ];
					long externalInquiries[] = new long[ 3 ];

					internalLogicalFiles[ 0] = rs.getLong( 17 );
					internalLogicalFiles[ 1] = rs.getLong( 18 );
					internalLogicalFiles[ 2] = rs.getLong( 19 );

					externalInterfaceFiles[ 0] = rs.getLong( 20 );
					externalInterfaceFiles[ 1] = rs.getLong( 21 );
					externalInterfaceFiles[ 2] = rs.getLong( 22 );

					externalInputs[ 0] = rs.getLong( 23 );
					externalInputs[ 1] = rs.getLong( 24 );
					externalInputs[ 2] = rs.getLong( 25 );

					externalOutputs[ 0] = rs.getLong( 26 );
					externalOutputs[ 1] = rs.getLong( 27 );
					externalOutputs[ 2] = rs.getLong( 28 );

					externalInquiries[ 0] = rs.getLong( 29 );
					externalInquiries[ 1] = rs.getLong( 30 );
					externalInquiries[ 2] = rs.getLong( 31 );

					int ratings[] = new int[ 18 ];
					int percent[] = new int[ 18 ];

					int x = 32;

					for ( int i = 0 ; i < 18 ; i++ )
					{
						ratings[i] = rs.getInt( x++ );
						percent[i] = rs.getInt( x++ );
					}

					long subComponentID = rs.getLong( 68 );

					// Create a sub component
					COINCOMOSubComponent subComponent = new COINCOMOSubComponent();

					// Set Parameters
					subComponent.setUnitID( subComponentID );
					subComponent.setParentID( component.getUnitID() );
					subComponent.setName( name );
					subComponent.setLaborRate( laborRate );
					subComponent.setBreakage( breakage );
					subComponent.setSizingType( sizeType );
					subComponent.setLanguage( language );
					subComponent.setSLOC( SLOC );
					subComponent.setInitialSLOC( initialSLOC );
					subComponent.setDesignModified( designModified );
					subComponent.setCodeModified( codeModified );
					subComponent.setIntegrationModified( integrationModified );
					subComponent.setSoftwareUnderstanding( softwareUnderstanding );
					subComponent.setAssementAssimilation( assessment );
					subComponent.setUnfamiliarity( unfamiliarity );
					subComponent.setAutomaticallyTranslated( automaticallyTranslated );
					subComponent.setAutoTranslationProductivity( autoTranslation );
					subComponent.setChangeMultiplier( change );
					subComponent.setInternalLogicalFiles( internalLogicalFiles );
					subComponent.setExternalInterfaceFiles( externalInterfaceFiles );
					subComponent.setExternalInputs( externalInputs );
					subComponent.setExternalOutputs( externalOutputs );
					subComponent.setExternalInquiries( externalInquiries );
					subComponent.setRatings( ratings );
					subComponent.setPercent( percent );

					// Update Generated Fields
					subComponent.setEaf( COINCOMOSubComponentManager.get_EAF( subComponent.getUnitID() , connection ) );
					subComponent.setCost( COINCOMOSubComponentManager.get_COST( subComponent.getUnitID() , connection ) );
					subComponent.setInstCost( COINCOMOSubComponentManager.get_INST_Cost( subComponent.getUnitID() , connection ) );
					subComponent.setProd( COINCOMOSubComponentManager.get_PROD( subComponent.getUnitID() , connection ) );
					subComponent.setStaff( COINCOMOSubComponentManager.get_Staff( subComponent.getUnitID() , connection ) );
					subComponent.setEstEffort( COINCOMOSubComponentManager.get_Estimated_Effort( subComponent.getUnitID() , connection ) );
					subComponent.setNomEffort( COINCOMOSubComponentManager.get_Nominal_Effort( subComponent.getUnitID() , connection ) );
					subComponent.setAdaptiveSLOC( COINCOMOSubComponentManager.get_Adaptive_Size( subComponent.getUnitID() , connection ) );
					subComponent.setAdaptationAdjustmentFactor( COINCOMOSubComponentManager.get_Adaptive_Adjustment_Factor( subComponent.getUnitID() , connection ) );

					// Add to the List
					component.addSubUnit( subComponent );
				}

				// Release From Memory
				preparedStatement.close();
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();

				return false;
			}
		}

		return true;
	}

	//Code by Vishesh, Sheryl, Deepak, Ankoor.........

	public static boolean insertCompCocotProject ( long componentID )
	{
		boolean isSuccessful = false;
		deleteTempCocotsTables();
		deleteTempSubCocotsTables();

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

		if ( connection != null )
		{
			try
			{
				String sql = "select cocots_insert_project_func(" + componentID + ");";
				String sql2 = "select cocots_get_proj_id();";


				// Efficient Way of Updating
				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
				PreparedStatement preparedStatement2 = (PreparedStatement) connection.prepareStatement( sql2 );


				// Update
				preparedStatement.executeQuery();
				preparedStatement2.executeQuery();


				// Free From Memory
				preparedStatement.close();
				preparedStatement2.close();


				// Update Status
				isSuccessful = true;
			}
			catch ( SQLException e )
			{
				// Print Any Problems ..
				e.printStackTrace();
				JOptionPane.showMessageDialog( COINCOMO.application, "Database Error", "Notice", JOptionPane.WARNING_MESSAGE );
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}
		}


		return isSuccessful;
	}

	public static boolean insertSubCompTemp ( long componentID )
	{
		boolean isSuccessful = false;


		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

		if ( connection != null )
		{
			try
			{
				String sql = "INSERT INTO cocots_temp_subcomponent(temp) VALUES (" + componentID + ");";

				// Efficient Way of Updating
				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );


				// Update
				preparedStatement.executeUpdate();

				// Free From Memory
				preparedStatement.close();

				// Update Status
				isSuccessful = true;
			}
			catch ( SQLException e )
			{
				// Print Any Problems ..
				e.printStackTrace();
				JOptionPane.showMessageDialog( COINCOMO.application, "Database Error", "Notice", JOptionPane.WARNING_MESSAGE );
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}
		}


		return isSuccessful;
	}
	
	public static boolean updateCompCocotProject ( COINCOMOComponent component )
	{
		boolean isSuccessful = false;


		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

		if ( connection != null )
		{
			try
			{

				String sql = "update cocots_projects_table " +
				"set project_name = ? " +
						"where project_id = ?";

				// Efficient Way of Updating
				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );

				// Replace "?" With Respective Values ..
				preparedStatement.setString( 1 , component.getName() );


				preparedStatement.setLong( 2 , component.getUnitID() );

				// Update
				preparedStatement.executeUpdate();



				// Free From Memory
				preparedStatement.close();


				// Update Status
				isSuccessful = true;
			}
			catch ( SQLException e )
			{
				// Print Any Problems ..
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}

		}

		return isSuccessful;
	}
	
	public static boolean insertCocotProject( COINCOMOComponent component )
	{
		boolean isSuccessful = false;
		deleteTempCocotsTables();

		// If Exists
		if ( component != null )
		{
			DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

			if ( connection != null )
			{
				try
				{
					String sql = "insert into public.cocots_projects_table (project_name,subsystem_id,originator,proj_date) values ('" + component.getProjectName() +  
					"'," + component.getUnitID() + ",'" + component.getProjectOriginator() + 
					"','" + component.getDate() +"');";
					String sql2 = "select cocots_get_proj_id();";
					String sqlProjectID = "select * from cocots_temp;";


					// Efficient Way of Updating
					PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
					PreparedStatement preparedStatement2 = (PreparedStatement) connection.prepareStatement( sql2 );
					PreparedStatement preparedStatementProjectID = (PreparedStatement) connection.prepareStatement(sqlProjectID);


					// Update
					preparedStatement.executeUpdate();
					preparedStatement2.executeQuery();
					ResultSet rs = preparedStatementProjectID.executeQuery();

					while ( rs != null && rs.next() )
					{
						rs.getInt(1);
						rs.getString(1);
						component.setProjectID(rs.getInt(1));
					}


					// Free From Memory
					preparedStatement.close();
					preparedStatement2.close();
					preparedStatementProjectID.close();


					// Update Status
					isSuccessful = true;
				}
				catch ( SQLException e )
				{
					// Print Any Problems ..
					e.printStackTrace();
					JOptionPane.showMessageDialog( COINCOMO.application, "Database Error", "Notice", JOptionPane.WARNING_MESSAGE );
				}
				finally
				{
					// Disconnect
					COINCOMODatabaseManager.disconnect( connection );
				}
			}
		}

		return isSuccessful;
	}

	public static boolean insertCocotsCots( COINCOMOComponent component )
	{
		boolean isSuccessful = false;
		// If Exists
		if ( component != null )
		{
			DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

			if ( connection != null )
			{
				try
				{
					ArrayList<Integer> cotIDs = component.getCotClassID();
					for ( Integer cotID : cotIDs )
					{
						String sql = "select cocots_insert_cots(" + cotID.intValue() + ");";

						// Efficient Way of Updating
						PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );


						// Update
						preparedStatement.executeQuery();


						// Free From Memory
						preparedStatement.close();


						// Update Status
						isSuccessful = true;
					}
				}
				catch ( SQLException e )
				{
					// Print Any Problems ..
					e.printStackTrace();
				}
				finally
				{
					// Disconnect
					COINCOMODatabaseManager.disconnect( connection );
				}
			}
		}

		return isSuccessful;
	}

	public static boolean insertGlueCost ( COINCOMOComponent component )
	{
		boolean isSuccessful = false;
		//component.get
		HashMap< Integer, int[] > costfactormap;
		costfactormap=component.getCocotsRatings();
		int i = 0;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
		if ( connection != null )
		{
			try
			{
				Iterator it = costfactormap.entrySet().iterator();

				while(it.hasNext())
				{
					Map.Entry pairs = (Map.Entry)it.next();
					int cot_id = ((Integer)pairs.getKey()).intValue();
					int[] costfactor = (int[]) pairs.getValue();


					String sqlInsert ="select cocots_glue_insert_inputs_func(" + cot_id + ","+ costfactor[0]+","+ costfactor[1]+","
							+ costfactor[2]+","+ costfactor[3]+","+ costfactor[4]+","+ costfactor[5]+","
							+ costfactor[6]+","+ costfactor[7]+","+ costfactor[8]+","+ costfactor[9]+","+costfactor[10]+","
							+ component.getKSLOC() + "," + component.getCREVOL() + ");";

					String sqlComp = "select cocots_glue_insert_comp_func(" + cot_id + ","+ costfactor[11]+","+ costfactor[12]+
					","+ costfactor[13]+","+ costfactor[14]+","+ costfactor[15]+","+ costfactor[16] + ");";

					String sqlCompFunc = "select cocots_glue_complexity_func(" + cot_id + ");";


					// Efficent Deletion
					PreparedStatement preparedStatement1 = (PreparedStatement) connection.prepareStatement( sqlInsert );
					PreparedStatement preparedStatement2 = (PreparedStatement) connection.prepareStatement( sqlComp );
					PreparedStatement preparedStatement3 = (PreparedStatement) connection.prepareStatement( sqlCompFunc );



					preparedStatement1.executeQuery();
					preparedStatement2.executeQuery();
					preparedStatement3.executeQuery();


					preparedStatement1.close();
					preparedStatement2.close();
					preparedStatement3.close();



					i++;


				}


				// Update Status
				isSuccessful = true;
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}


		}
		return isSuccessful;
	}

	public static boolean insertAssessCost ( COINCOMOComponent component )
	{
		boolean isSuccessful = false;
		//component.get
		HashMap< Integer, int[] > costfactormap;
		costfactormap=component.getAssessCocotsRatings();
		int i = 0;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
		if ( connection != null )
		{
			try
			{
				Iterator it = costfactormap.entrySet().iterator();

				while(it.hasNext())
				{
					Map.Entry pairs = (Map.Entry)it.next();
					int cot_id = ((Integer)pairs.getKey()).intValue();
					int[] costfactor = (int[]) pairs.getValue();


					String sqlInsert ="select cocots_assess_insert_inputs_func(" + cot_id + "," + costfactor[17]+ "," + costfactor[18] +","
							+ costfactor[0] + "," + costfactor[1] + ","
							+ costfactor[2] + "," + costfactor[3] + "," + costfactor[4] + "," + costfactor[5]+","
							+ costfactor[6] + "," + costfactor[7] + "," + costfactor[8] + "," + costfactor[9]+","
							+ costfactor[10] + "," + costfactor[11] + "," + costfactor[12] + "," 
							+ costfactor[13] + "," + costfactor[14] + "," + costfactor[15] + "," 
							+ costfactor[16] + ");";



					// Efficent Deletion
					PreparedStatement preparedStatement1 = (PreparedStatement) connection.prepareStatement( sqlInsert );

					preparedStatement1.executeQuery();

					preparedStatement1.close();


					i++;


				}


				// Update Status
				isSuccessful = true;
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}


		}
		return isSuccessful;
	}

	public static boolean insertTailCost ( COINCOMOComponent component )
	{
		boolean isSuccessful = false;
		//component.get
		HashMap< Integer, double[] > costfactormap;
		costfactormap= component.getTailCocotsRatings();
		int i = 0;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
		if ( connection != null )
		{
			try
			{
				Iterator it = costfactormap.entrySet().iterator();

				while(it.hasNext())
				{
					Map.Entry pairs = (Map.Entry)it.next();
					int cot_id = ((Integer)pairs.getKey()).intValue();
					double[] costfactor = (double[]) pairs.getValue();


					String sqlInsert ="select cocots_tail_insert_inputs_func(" + cot_id + ","+ ((int)costfactor[6])+","+ (int)costfactor[0]+","
							+ (int)costfactor[1]+","+ (int)costfactor[2]+","+ (int)costfactor[3]+","+ (int)costfactor[4]+","
							+ (int)costfactor[5]+","+ (int)costfactor[6]+","+ costfactor[7] + ");";

					String sqlTcq = "select cocots_tail_tcq_func(" + cot_id + ");";


					// Efficent Deletion
					PreparedStatement preparedStatement1 = (PreparedStatement) connection.prepareStatement( sqlInsert );
					PreparedStatement preparedStatement2 = (PreparedStatement) connection.prepareStatement( sqlTcq );



					preparedStatement1.executeQuery();
					preparedStatement2.executeQuery();


					preparedStatement1.close();
					preparedStatement2.close();



					i++;


				}


				// Update Status
				isSuccessful = true;
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}


		}
		return isSuccessful;
	}

	public static boolean insertCocotCost(COINCOMOComponent component)
	{
		boolean isSuccessful = false;
		//component.get
		HashMap< Integer, int[] > costfactormap;
		costfactormap=component.getCocotsRatings();
		int i = 0;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
		if ( connection != null )
		{
			try
			{
				Iterator it = costfactormap.entrySet().iterator();


				while(it.hasNext())
				{
					Map.Entry pairs = (Map.Entry)it.next();
					int cot_id = ((Integer)pairs.getKey()).intValue();
					int[] costfactor = (int[]) pairs.getValue();

					String sql1 ="select cocots_insert_cots(" + cot_id + ");";

					String sql2 ="select cocots_insert_inputs(" + cot_id + ","+ costfactor[0]+","+ costfactor[1]+","
					+ costfactor[2]+","+ costfactor[3]+","+ costfactor[4]+","+ costfactor[5]+","
					+ costfactor[6]+","+ costfactor[7]+","+ costfactor[8]+","+ costfactor[9]+","+costfactor[10]+","
					+ component.getKSLOC() + "," + component.getCREVOL() + ");";

					String sql3 = "select cocots_insert_comp(" + cot_id + ","+ costfactor[11]+","+ costfactor[12]+
					","+ costfactor[13]+","+ costfactor[14]+","+ costfactor[15]+","+ costfactor[16] + ");";

					String sql4 = "select cocots_complexity_func(" + cot_id + ");";


					// Efficent Deletion
					PreparedStatement preparedStatement1 = (PreparedStatement) connection.prepareStatement( sql1 );
					PreparedStatement preparedStatement2 = (PreparedStatement) connection.prepareStatement( sql2 );
					PreparedStatement preparedStatement3 = (PreparedStatement) connection.prepareStatement( sql3 );
					PreparedStatement preparedStatement4 = (PreparedStatement) connection.prepareStatement( sql4 );



					preparedStatement1.executeQuery();
					preparedStatement2.executeQuery();
					preparedStatement3.executeQuery();
					preparedStatement4.executeQuery();


					preparedStatement1.close();
					preparedStatement2.close();
					preparedStatement3.close();
					preparedStatement4.close();



					i++;


				}


				// Update Status
				isSuccessful = true;
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}


		}
		return isSuccessful;
	}

	public static boolean updateProjectResult(COINCOMOComponent component)
	{
		boolean isSuccessful = false;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
		if ( connection != null )
		{
			try
			{
				component.clearCotClass();
				String sql5 = "select cocots_calc_func();";
				PreparedStatement preparedStatement5 = (PreparedStatement) connection.prepareStatement( sql5 );
				preparedStatement5.executeQuery();
				preparedStatement5.close();

				String sql = "select proj_result from cocots_projects_table where project_id = " + component.getUnitID() +";";

				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );

				ResultSet rs = preparedStatement.executeQuery();
				while ( rs != null && rs.next() )
				{
					component.setProjectResult(rs.getDouble( 1 ));

				}

				//preparedStatement.executeUpdate();

				preparedStatement.close();

				deleteTempCocotsTables();
				deleteTempSubCocotsTables();




				// Update Status
				isSuccessful = true;

			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}


		}
		return isSuccessful;
	}

	public static boolean deleteTempCocotTables ()
	{
		boolean isSuccessful = false;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

		// Check if connection exists
		if ( connection != null )
		{
			try
			{
				String deleteCocotTemp = "delete from cocots_temp";
				String deleteCocotGlueUpdate = "delete from cocots_glue_update_input_table";
				String deleteCocotAssessUpdate = "delete from cocots_assess_update_input_table";
				String deleteCocotTailUpdate = "delete from cocots_tail_update_input_table";

				// Efficent Deletion
				PreparedStatement preparedStatementDeleteCocotTemp = (PreparedStatement) connection.prepareStatement( deleteCocotTemp );
				PreparedStatement preparedStatementDeleteCocotGlue = (PreparedStatement) connection.prepareStatement( deleteCocotGlueUpdate );
				PreparedStatement preparedStatementDeleteCocotAssess = (PreparedStatement) connection.prepareStatement( deleteCocotAssessUpdate );
				PreparedStatement preparedStatementDeleteCocotTail = (PreparedStatement) connection.prepareStatement( deleteCocotTailUpdate );

				// Delete
				preparedStatementDeleteCocotTemp.executeUpdate();
				preparedStatementDeleteCocotGlue.executeUpdate();
				preparedStatementDeleteCocotAssess.executeUpdate();
				preparedStatementDeleteCocotTail.executeUpdate();

				// Release From Memory
				preparedStatementDeleteCocotTemp.close();
				preparedStatementDeleteCocotGlue.close();
				preparedStatementDeleteCocotAssess.close();
				preparedStatementDeleteCocotTail.close();


				// Update Status
				isSuccessful = true;
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}
		}

		return isSuccessful;
	}

	public static boolean deleteCocotsTablesFinal()
	{
		boolean isSuccessful = false;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

		// Check if connection exists
		if ( connection != null )
		{
			try
			{
				String deleteCocotFinal = "select cocots_update_delete_all_func();";

				// Efficent Deletion
				PreparedStatement preparedStatementDeleteCocotFinal = (PreparedStatement) connection.prepareStatement( deleteCocotFinal );

				// Delete
				preparedStatementDeleteCocotFinal.executeQuery();

				// Release From Memory
				preparedStatementDeleteCocotFinal.close();


				// Update Status
				isSuccessful = true;
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}
		}

		return isSuccessful;
	}

	public static Vector<String> loadCocotsProjects()
	{
		boolean isSuccessful = false;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
		Vector<String> projects = null;

		if ( connection != null )
		{
			try
			{
				String sql = "SELECT project_name FROM cocots_projects_table;";

				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );

				ResultSet rs = preparedStatement.executeQuery();
				projects = new Vector<String>();
				while ( rs != null && rs.next() )
				{
					projects.add(rs.getString(1));
				}

				preparedStatement.close();

				// Update Status
				isSuccessful = true;

			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}


		}
		return projects;
	}

	public static boolean loadCocotProject ( String projectName , COINCOMOComponent component )
	{
		deleteTempCocotTables();

		boolean isSuccessful = false;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
		if ( connection != null )
		{
			try
			{
				String sql = "select cocots_get_update_proj_id_func('" + projectName+"');";
				String sqlProjectID = "SELECT temp FROM cocots_temp;";
				// Efficient Way of Updating
				PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
				PreparedStatement preparedStatementProjID = (PreparedStatement) connection.prepareStatement( sqlProjectID );

				ResultSet rs = preparedStatement.executeQuery();
				ResultSet rsProjID = preparedStatementProjID.executeQuery();

				while ( rsProjID != null && rsProjID.next() )
				{
					int projID = rsProjID.getInt( 1 );
					component.setProjectID(projID);
				}

				preparedStatement.close();
				preparedStatementProjID.close();

				String sqlLoadCocotParams = "SELECT project_id, project_name, " +
				"subsystem_id, originator, proj_date, proj_result " +
				"FROM cocots_projects_table WHERE project_id = " + component.getProjectID() + ";";

				PreparedStatement preparedStatementLoadParams = (PreparedStatement) connection.prepareStatement( sqlLoadCocotParams );
				ResultSet rsLoadParams = preparedStatementLoadParams.executeQuery();
				while ( rsLoadParams != null && rsLoadParams.next() )
				{
					String projName = rsLoadParams.getString( 2 );
					String origName = rsLoadParams.getString( 4 );
					String date = rsLoadParams.getString(5 );
					double projResult = rsLoadParams.getDouble( 6 );

					component.setProjectName(projName);
					component.setProjectOriginator(origName);
					component.setDateString(date);
					component.setProjectResult(projResult);

				}

				preparedStatementLoadParams.close();

				String sqlUpdateSelect = "SELECT cocots_update_select_func();";
				PreparedStatement preparedStatementUpdateSelect = (PreparedStatement) connection.prepareStatement( sqlUpdateSelect );
				ResultSet rsUpdateSelect = preparedStatementUpdateSelect.executeQuery();
				preparedStatementUpdateSelect.close();


				// Update Status
				isSuccessful = true;

			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();

			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}


		}
		return isSuccessful;

	}

	public static boolean loadCocotGlue (COINCOMOComponent component)
	{
		boolean isSuccessful = false;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
		if ( connection != null )
		{
			try
			{

				String sqlLoadInputs = "SELECT project_id, cots_id, aciep, acipc, axicp, apcon, acpmt, acsew, " +
				"intconvap, ctrlap, datap, acpps, acptd, acrel, intconvaa, ctrlaa, dataa, " +
				"acper, asprt, ksloc, crevol " +
				"FROM cocots_glue_update_input_table;";

				PreparedStatement preparedStatementLoadInputs = (PreparedStatement) connection.prepareStatement( sqlLoadInputs );
				ResultSet rsLoadInputs = preparedStatementLoadInputs.executeQuery();


				while ( rsLoadInputs != null && rsLoadInputs.next() )
				{
					Integer cocot_id = new Integer(rsLoadInputs.getInt( 2 ));
					if(cocot_id.intValue() == 0)
						component.setAggregateLoaded();
					int ratings[] = new int[17];

					ratings[0] = rsLoadInputs.getInt( 3 ); //aciep
					ratings[1] = rsLoadInputs.getInt( 4 ); //acipc
					ratings[2] = rsLoadInputs.getInt( 5 ); //axicp
					ratings[3] = rsLoadInputs.getInt( 6 ); //apcon
					ratings[4] = rsLoadInputs.getInt( 7 ); //acpmt
					ratings[5] = rsLoadInputs.getInt( 8 ); //acsew

					ratings[6] = rsLoadInputs.getInt( 9 ); //acpps
					ratings[7] = rsLoadInputs.getInt( 10 ); //acptd
					ratings[8] = rsLoadInputs.getInt( 11 ); //acrel

					ratings[9] = rsLoadInputs.getInt( 12 ); //acper
					ratings[10] = rsLoadInputs.getInt( 13 ); //asprt

					ratings[11] = rsLoadInputs.getInt( 14 ); //apcpx
					ratings[12] = rsLoadInputs.getInt( 15 ); //apcpx
					ratings[13] = rsLoadInputs.getInt( 16 ); //apcpx

					ratings[14] = rsLoadInputs.getInt( 17 ); //apcpx
					ratings[15] = rsLoadInputs.getInt( 18 ); //apcpx
					ratings[16] = rsLoadInputs.getInt( 19 ); //apcpx

					component.setKSLOC(rsLoadInputs.getDouble( 20 ));
					component.setCREVOL(rsLoadInputs.getDouble( 21 ));


					component.setLoadedCocotRatings(cocot_id, ratings);
				}





				preparedStatementLoadInputs.close();


				// Update Status
				isSuccessful = true;

			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}


		}
		return isSuccessful;
	}

	public static boolean loadCocotAssess (COINCOMOComponent component)
	{
		boolean isSuccessful = false;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
		if ( connection != null )
		{
			try
			{

				String sqlLoadInputs = "SELECT project_id, cots_id, nocots_in, nocots_det, corr, avail, secur, " +
				"prod, under, eou, ver, intercomp, flex, install, port, func, " +
				"price, mat, vend, train, vendcon " +
				"FROM cocots_assess_update_input_table;";

				PreparedStatement preparedStatementLoadInputs = (PreparedStatement) connection.prepareStatement( sqlLoadInputs );
				ResultSet rsLoadInputs = preparedStatementLoadInputs.executeQuery();


				while ( rsLoadInputs != null && rsLoadInputs.next() )
				{
					Integer cocot_id = new Integer(rsLoadInputs.getInt( 2 ));
					if(cocot_id.intValue() == 0)
						component.setAggregateLoaded();
					int ratings[] = new int[19];

					//component.setNumOfIntialAssessCots( rsLoadInputs.getInt( 3 ) ); //nocots_in
					//component.setNumOfFinalAssessCots( rsLoadInputs.getInt( 4 ) ); //nocots_det

					ratings[0] = rsLoadInputs.getInt( 5 ); //corr
					ratings[1] = rsLoadInputs.getInt( 6 ); //avail
					ratings[2] = rsLoadInputs.getInt( 7 ); //secur
					ratings[3] = rsLoadInputs.getInt( 8 ); //prod

					ratings[4] = rsLoadInputs.getInt( 9 ); //under
					ratings[5] = rsLoadInputs.getInt( 10 ); //eou
					ratings[6] = rsLoadInputs.getInt( 11 ); //ver

					ratings[7] = rsLoadInputs.getInt( 12 ); //intercomp
					ratings[8] = rsLoadInputs.getInt( 13 ); //flex

					ratings[9] = rsLoadInputs.getInt( 14 ); //install
					ratings[10] = rsLoadInputs.getInt( 15 ); //port
					ratings[11] = rsLoadInputs.getInt( 16 ); //func

					ratings[12] = rsLoadInputs.getInt( 17 ); //price
					ratings[13] = rsLoadInputs.getInt( 18 ); //mat
					ratings[14] = rsLoadInputs.getInt( 19 ); //vend
					ratings[15] = rsLoadInputs.getInt( 20 ); //train
					ratings[16] = rsLoadInputs.getInt( 21 ); //vendcon

					ratings[17] = rsLoadInputs.getInt( 3 ); //nocots_in
					ratings[18] = rsLoadInputs.getInt( 4 ); //nocots_det



					component.setLoadedAssessCocotRatings(cocot_id, ratings);
				}





				preparedStatementLoadInputs.close();


				// Update Status
				isSuccessful = true;

			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}


		}
		return isSuccessful;
	}

	public static boolean loadCocotTail (COINCOMOComponent component)
	{
		boolean isSuccessful = false;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
		if ( connection != null )
		{
			try
			{

				String sqlLoadInputs = "SELECT project_id, cots_id, cots_num, param_spec, script_w, io_report, " +
				"gui_ssl, sec_apis, avail_tools, tcq, tail_effort, median_tail_effort " +
				"FROM cocots_tail_update_input_table;";

				PreparedStatement preparedStatementLoadInputs = (PreparedStatement) connection.prepareStatement( sqlLoadInputs );
				ResultSet rsLoadInputs = preparedStatementLoadInputs.executeQuery();


				while ( rsLoadInputs != null && rsLoadInputs.next() )
				{
					Integer cocot_id = new Integer(rsLoadInputs.getInt( 2 ));
					if(cocot_id.intValue() == 0)
						component.setAggregateLoaded();
					double ratings[] = new double[10];

					component.setNumOfTailCots( rsLoadInputs.getInt( 3 ) ); //cots_num

					ratings[0] = rsLoadInputs.getInt( 4 ); //param_spec
					ratings[1] = rsLoadInputs.getInt( 5 ); //script_w
					ratings[2] = rsLoadInputs.getInt( 6 ); //io_report
					ratings[3] = rsLoadInputs.getInt( 7 ); //gui_ssl

					ratings[4] = rsLoadInputs.getInt( 8 ); //sec_apis
					ratings[5] = rsLoadInputs.getInt( 9 ); //avail_tools
					ratings[6] = rsLoadInputs.getInt( 10 ); //tcq

					ratings[7] = rsLoadInputs.getInt( 11 ); //tail_effort

					ratings[8] = rsLoadInputs.getInt( 3 );
					ratings[9] = rsLoadInputs.getDouble( 12 );

					component.setTailMedianDefault( rsLoadInputs.getInt( 12 ) ); //median_tail_effort


					component.setLoadedTailCocotRatings(cocot_id, ratings);
				}





				preparedStatementLoadInputs.close();


				// Update Status
				isSuccessful = true;

			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}


		}
		return isSuccessful;
	}

	public static boolean loadMedianDefault ( int cot_id , COINCOMOComponent component )
	{
		boolean isSuccessful = false;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
		if ( connection != null )
		{
			try
			{

				String sqlLoadInputs = "SELECT median_user_def " +
				"FROM cocots_tail_medtail_table " +
				"WHERE cots_id=" + cot_id + ";";

				PreparedStatement preparedStatementLoadInputs = (PreparedStatement) connection.prepareStatement( sqlLoadInputs );
				ResultSet rsLoadInputs = preparedStatementLoadInputs.executeQuery();

				while ( rsLoadInputs != null && rsLoadInputs.next() )
				{
					component.setTailMedianDefault( rsLoadInputs.getDouble( 1 ) );
				}

				preparedStatementLoadInputs.close();


				// Update Status
				isSuccessful = true;

			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}


		}
		return isSuccessful;
	}

	public static boolean deleteTempCocotsTables ()
	{
		boolean isSuccessful = false;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

		// Check if connection exists
		if ( connection != null )
		{
			try
			{
				String deleteCocotTemp = "DELETE FROM cocots_temp";

				// Efficent Deletion
				PreparedStatement preparedStatementDeleteCocotTemp = (PreparedStatement) connection.prepareStatement( deleteCocotTemp );

				// Delete
				preparedStatementDeleteCocotTemp.executeUpdate();

				// Release From Memory
				preparedStatementDeleteCocotTemp.close();



				// Update Status
				isSuccessful = true;
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}
		}

		return isSuccessful;
	}

	public static boolean deleteTempSubCocotsTables ()
	{
		boolean isSuccessful = false;

		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

		// Check if connection exists
		if ( connection != null )
		{
			try
			{
				String deleteCocotTemp = "DELETE FROM cocots_temp_subcomponent";

				// Efficent Deletion
				PreparedStatement preparedStatementDeleteCocotTemp = (PreparedStatement) connection.prepareStatement( deleteCocotTemp );

				// Delete
				preparedStatementDeleteCocotTemp.executeUpdate();

				// Release From Memory
				preparedStatementDeleteCocotTemp.close();



				// Update Status
				isSuccessful = true;
			}
			catch ( SQLException e )
			{
				// Print any Problems ...
				e.printStackTrace();
			}
			finally
			{
				// Disconnect
				COINCOMODatabaseManager.disconnect( connection );
			}
		}

		return isSuccessful;
	}

}
